package com.iflysse.servlet.content;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.iflysse.pojo.ColumnInfo;
import com.iflysse.pojo.ContentInfo;
import com.iflysse.service.ColumnInfoService;
import com.iflysse.service.ContentInfoService;

/**
 * Servlet implementation class SaveContentServlet
 */
@WebServlet("/content/SaveContentServlet.do")
public class SaveContentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SaveContentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    request.setCharacterEncoding("utf-8");
	    response.setCharacterEncoding("utf-8");
	    
	    String nav = request.getParameter("nav");
	    if(nav != null){
	    	ColumnInfoService service = new ColumnInfoService();
	    	List<ColumnInfo> list = service.findAllColumn();
	    	List<ColumnInfo> colist = new ArrayList<ColumnInfo>();
	    	for(ColumnInfo col:list){
	    		if(col.getPpid() != 1 && col.getPpid() != 0){
	    			if(col.getStatus().equals("Y")){
	    				colist.add(col);
	    			}
	    		}
	    	}
	    	PrintWriter out=response.getWriter();
			out.println(colist);
			out.flush();
			out.close();
	    } else{
			String id=request.getParameter("id");
		    String titleName=request.getParameter("titleName");
		    String states=request.getParameter("states");
		    String keywords=request.getParameter("keywords");
		    String content=request.getParameter("content");
		    String ordersc = request.getParameter("orderscore");
		    String name = request.getParameter("columnName");
		    int orderscore = Integer.parseInt(ordersc);
		    ContentInfoService  service=new ContentInfoService();
		    
		    ContentInfo contentInfo=new ContentInfo();
	    	contentInfo.setTitle(titleName);
	    	contentInfo.setConstatus(states);
	    	contentInfo.setKeyword(keywords);
	    	contentInfo.setContent(content);
	    	contentInfo.setOrderscore(orderscore);
	    	contentInfo.setColumnname(name);
		    
		    if(StringUtils.isNotEmpty(id)) //当前为编辑内容
		    {
		      contentInfo.setId(Integer.parseInt(id));
		      service.updateContent(contentInfo);	
		    }else //当前为新增内容
		    {
		    	service.insertContent(contentInfo);
		    }
		    response.sendRedirect(request.getContextPath()+"/content/ContentOptRequestServlet.do");
	    }
	}

}
